import config from './config'

/**
 * OA后台项目 POST
 */
function POST(url, params) {
  // 加载loading
  wx.showLoading({
    title: '加载中...',
    mask: true
  })

  let promise = new Promise(function (resolve, reject) {
    wx.request({
      url: config.host +'/oa'+ url,
      data: JSON.stringify(params),
      header: {
        "Content-Type": "application/json;charset=utf-8",
      },
      method: 'POST',
      success: (res) => {
        resolve(res)
      },
      fail: (res) => {
        reject(res)
      },
      complete: () => {
        setTimeout(function () {
          wx.hideLoading()
        }, 1000)
      }
    })
  });
  return promise;
}

/**
 * OA后台项目 GET
 */
function GET(url, params) {

  let promise = new Promise(function (resolve, reject) {
    wx.request({
      url: config.host +'/oa'+ url,
      data: params,
      header: {
        "Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
      },
      method: 'GET',
      success: (res) => {
        resolve(res)
      },
      fail: (res) => {
        reject(res)
      },
      complete: () => {
        setTimeout(function () {
          wx.hideLoading();
        }, 1000)
      }
    })
  });
  return promise
}

/**
 * 用户服务项目 POST
 */
function USER_POST(url, params) {
  // 加载loading
  wx.showLoading({
    title: '加载中...',
    mask: true
  })

  let promise = new Promise(function (resolve, reject) {
    wx.request({
      url: config.userHost +'/userver'+ url,
      data: params,
      header: {
        "Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
      },
      method: 'POST',
      success: (res) => {
        resolve(res)
      },
      fail: (res) => {
        reject(res)
      },
      complete: () => {
        setTimeout(function () {
          wx.hideLoading()
        }, 1000)
      }
    })
  });
  return promise;
}

module.exports = {
  POST: POST,
  GET: GET,
  USER_POST: USER_POST
}